package com.yindong.camera.slice;

import com.yindong.camera.ResourceTable;
import com.yindong.camera.slice.init.CameraInit;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.StackLayout;
import ohos.agp.components.surfaceprovider.SurfaceProvider;
import ohos.agp.graphics.Surface;
import ohos.agp.graphics.SurfaceOps;
import ohos.agp.utils.LayoutAlignment;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.common.ImageFormat;

/**
 * 相机业务层
 *
 * @author      殷冬
 * @date        2021-01-31
 * @version     1.0.0
 */
public class CameraService {

    private static final HiLogLabel LOG = new HiLogLabel(HiLog.LOG_APP, 00201, CameraService.class.getName());

    /**
     * abilitySlice对象
     */
    private static AbilitySlice abilitySlice;

    /**
     * 相机初始化
     *
     * @param abilitySlice      abilitySlice对象
     */
    public static synchronized void initCameraService(AbilitySlice abilitySlice){
        CameraService cameraService = new CameraService();
        CameraService.abilitySlice = abilitySlice;
        // 初始化相机配置
        CameraInfoConfig.initCameraConfig(abilitySlice.getContext());
        // 创建相机
        cameraService.createCamera();
    }

    /**
     * 创建相机
     */
    private void createCamera(){
        // 获取组件对象
//        SurfaceProvider surfaceProvider = (SurfaceProvider) abilitySlice.findComponentById(ResourceTable.Id_surface_provider);
        StackLayout stackLayout = (StackLayout) abilitySlice.findComponentById(ResourceTable.Id_stack_layout);
        SurfaceProvider surfaceProvider = new SurfaceProvider(abilitySlice.getContext());
        // 放在 AGP 容器组件的顶层
        surfaceProvider.pinToZTop(true);
        // 设置样式
        StackLayout.LayoutConfig layoutConfig = new StackLayout.LayoutConfig();
        layoutConfig.alignment = LayoutAlignment.CENTER;
        layoutConfig.setMarginBottom(55);
        // 根据比例，设置相机长宽值
        layoutConfig.width = CameraInfoConfig.cameraSize.width;
        layoutConfig.height = CameraInfoConfig.cameraSize.height;
        surfaceProvider.setLayoutConfig(layoutConfig);
        // 将组件添加到容器中
        stackLayout.addComponent(surfaceProvider);
        // 获取SurfaceOps对象
        SurfaceOps surfaceOps = surfaceProvider.getSurfaceOps().get();
        // 设置像素格式
        surfaceOps.setFormat(ImageFormat.JPEG);
        // 设置屏幕一直打开
        surfaceOps.setKeepScreenOn(true);
        // 添加回调
        surfaceOps.addCallback(new SurfaceOpsCallBack());
        /*// 拍照按钮添加点击事件
        Button pictureButton = (Button) abilitySlice.findComponentById(ResourceTable.Id_picture_button);
        pictureButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                CameraInit.capture();
            }
        });*/

    }

    /**
     * surfaceOps回调实现类
     *
     * @author      殷冬
     * @date        2021-01-31
     * @version     1.0.0
     */
    class SurfaceOpsCallBack implements SurfaceOps.Callback{

        @Override
        public void surfaceCreated(SurfaceOps surfaceOps) {
            // 获取surface对象
            Surface surface = surfaceOps.getSurface();
            CameraInit cameraInit = new CameraInit(abilitySlice.getContext(), surface, surfaceOps);
            // 创建相机对象
            boolean state = cameraInit.createCamera();
            if(state){
                HiLog.info(LOG, "--------------------相机创建成功!");
            }else{
                HiLog.info(LOG, "--------------------相机创建失败!");
            }
        }

        @Override
        public void surfaceChanged(SurfaceOps surfaceOps, int i, int i1, int i2) {

        }

        @Override
        public void surfaceDestroyed(SurfaceOps surfaceOps) {

        }
    }

}
